<?php

namespace Tuobaye\FilamentAdmin\Concerns;

use Filament\Facades\Filament;
use Filament\Resources\Resource;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Str;
use Tuobaye\FilamentAdmin\PermissionMap;

/**
 * @mixin Resource
 */
trait WithPermissions
{

    protected static function getPermissionResource(): string
    {
        return static::class;
    }

    protected static function getPrefix(): string
    {
        preg_match('/^(\w+\\\\\w+\\\\).+?$/', static::class, $matches);
        $prefix = $matches[1];
        return str_replace('\\', ':', $prefix . Str::between(static::getPermissionResource(), 'Resources\\', 'Resource'));
    }


    public static function allPermissions(): array
    {
        $maps = array_merge(static::defaultPermissions(), static::getPermissions());

        $permissions = [];
        foreach ($maps as $key => $name) {
            $permissions[static::getPrefix() . '@' . $key] = $name;
        }
        return [static::getPrefix(), static::getModelLabel(), $permissions];

    }

    protected static function getHasPermissions(): array
    {
        $map = app(PermissionMap::class);
        if (!$map->hasSet) {
            $permissions = [];
            foreach (Filament::auth()->user()->roles as $role) {
                $permissions = array_merge($permissions, $role->permissions()->pluck('permission')->toArray());
            }
            $map->setPermissions($permissions);
        }
        return $map->getPermissions();
    }

    public static function getPermissions(): array
    {
        return [];
    }

    public static function defaultPermissions(): array
    {

        $result = [
            'list' => static::getModelLabel() . '列表',
            'create' => '创建' . static::getModelLabel(),
            'delete' => '删除' . static::getModelLabel(),
        ];
        if (static::hasPage('view')) {
            $result['view'] = static::getModelLabel() . '详情';
        }
        return $result;
    }

    public static function checkPermission($permission): bool
    {
        if (Filament::auth()->user()->is_super_admin) {
            return true;
        }

        $permissions = static::getHasPermissions();
        if (Str::contains($permission, '@')) {
            return in_array($permission, $permissions);
        } else {
            return in_array(static::getPrefix() . '@' . $permission, $permissions);
        }
    }

    public static function canCreate(): bool
    {
        return static::checkPermission('create');
    }

    public static function canView(Model $record): bool
    {
        return static::checkPermission('view');
    }

    public static function canDelete(Model $record): bool
    {
        return static::checkPermission('delete');
    }

    public static function canDeleteAny(): bool
    {
        return static::checkPermission('delete');
    }

    public static function canForceDelete(Model $record): bool
    {
        return static::checkPermission('delete');
    }

    public static function canForceDeleteAny(): bool
    {
        return static::checkPermission('delete');
    }

    public static function canEdit(Model $record): bool
    {
        return static::checkPermission('edit');
    }


    public static function canAccess(): bool
    {
        return static::checkPermission('list');
    }


    public static function shouldRegisterNavigation(): bool
    {
        return static::checkPermission('list');
    }

}
